using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 路径高亮系统使用示例 - 展示如何使用高亮和拖拽功能
/// </summary>
public class PathHighlightExample : MonoBehaviour
{
    [Header("UI设置")]
    public Button generatePathButton; // 生成路径按钮
    public Button clearPathButton; // 清空路径按钮
    public Button toggleHighlightButton; // 切换高亮按钮
    
    [Header("测试物品")]
    public GameObject testItemPrefab; // 测试物品预制体
    public Transform itemSpawnPoint; // 物品生成点
    
    // 组件引用
    private PathHighlightManager highlightManager;
    private ItemDragManager dragManager;
    private TilemapPathGenerator pathGenerator;
    
    private void Start()
    {
        // 获取组件引用
        highlightManager = FindObjectOfType<PathHighlightManager>();
        dragManager = FindObjectOfType<ItemDragManager>();
        pathGenerator = FindObjectOfType<TilemapPathGenerator>();
        
        // 设置按钮事件
        SetupButtons();
        
        // 生成测试物品
        SpawnTestItems();
    }
    
    /// <summary>
    /// 设置按钮事件
    /// </summary>
    private void SetupButtons()
    {
        if (generatePathButton != null)
        {
            generatePathButton.onClick.AddListener(OnGeneratePathClicked);
        }
        
        if (clearPathButton != null)
        {
            clearPathButton.onClick.AddListener(OnClearPathClicked);
        }
        
        if (toggleHighlightButton != null)
        {
            toggleHighlightButton.onClick.AddListener(OnToggleHighlightClicked);
        }
    }
    
    /// <summary>
    /// 生成路径按钮点击事件
    /// </summary>
    private void OnGeneratePathClicked()
    {
        if (pathGenerator != null)
        {
            pathGenerator.GeneratePath();
            Debug.Log("路径已生成");
        }
    }
    
    /// <summary>
    /// 清空路径按钮点击事件
    /// </summary>
    private void OnClearPathClicked()
    {
        if (highlightManager != null)
        {
            highlightManager.ClearPath();
            Debug.Log("路径已清空");
        }
    }
    
    /// <summary>
    /// 切换高亮按钮点击事件
    /// </summary>
    private void OnToggleHighlightClicked()
    {
        if (highlightManager != null)
        {
            // 这里可以添加切换高亮显示的逻辑
            Debug.Log("切换高亮显示");
        }
    }
    
    /// <summary>
    /// 生成测试物品
    /// </summary>
    private void SpawnTestItems()
    {
        if (testItemPrefab == null || itemSpawnPoint == null) return;
        
        // 生成3个测试物品
        for (int i = 0; i < 3; i++)
        {
            Vector3 spawnPosition = itemSpawnPoint.position + Vector3.right * i * 2f;
            GameObject testItem = Instantiate(testItemPrefab, spawnPosition, Quaternion.identity);
            
            // 确保物品有DraggableItem组件
            if (testItem.GetComponent<DraggableItem>() == null)
            {
                testItem.AddComponent<DraggableItem>();
            }
            
            // 设置物品名称
            testItem.name = $"TestItem_{i + 1}";
        }
    }
    
    /// <summary>
    /// 手动测试高亮功能
    /// </summary>
    [ContextMenu("测试高亮功能")]
    public void TestHighlightFunction()
    {
        if (highlightManager == null) return;
        
        // 创建测试路径
        var testPath = new System.Collections.Generic.List<Vector3Int>
        {
            new Vector3Int(0, 0, 0),
            new Vector3Int(1, 0, 0),
            new Vector3Int(2, 0, 0),
            new Vector3Int(2, 1, 0),
            new Vector3Int(2, 2, 0),
            new Vector3Int(1, 2, 0),
            new Vector3Int(0, 2, 0),
            new Vector3Int(0, 1, 0)
        };
        
        highlightManager.SetPath(testPath);
        Debug.Log("测试路径已设置，移动鼠标查看高亮效果");
    }
    
    /// <summary>
    /// 手动测试拖拽功能
    /// </summary>
    [ContextMenu("测试拖拽功能")]
    public void TestDragFunction()
    {
        if (dragManager == null) return;
        
        // 创建一个测试物品
        GameObject testItem = GameObject.CreatePrimitive(PrimitiveType.Cube);
        testItem.name = "DragTestItem";
        testItem.transform.position = Vector3.zero;
        
        // 添加拖拽组件
        if (testItem.GetComponent<DraggableItem>() == null)
        {
            testItem.AddComponent<DraggableItem>();
        }
        
        Debug.Log("测试物品已创建，可以拖拽测试");
    }
    
    /// <summary>
    /// 获取当前高亮状态信息
    /// </summary>
    [ContextMenu("获取高亮状态信息")]
    public void GetHighlightInfo()
    {
        if (highlightManager == null) return;
        
        Vector3Int hoveredCell = highlightManager.GetCurrentHoveredCell();
        Vector3 hoveredWorldPos = highlightManager.GetCurrentHoveredWorldPosition();
        bool isDragging = highlightManager.IsDragging;
        bool canPlace = highlightManager.CanPlaceItem;
        
        Debug.Log($"高亮状态信息:\n" +
                  $"悬停格子: {hoveredCell}\n" +
                  $"悬停世界坐标: {hoveredWorldPos}\n" +
                  $"是否拖拽中: {isDragging}\n" +
                  $"可放置: {canPlace}");
    }
    
    private void OnDestroy()
    {
        // 清理按钮事件
        if (generatePathButton != null)
        {
            generatePathButton.onClick.RemoveListener(OnGeneratePathClicked);
        }
        
        if (clearPathButton != null)
        {
            clearPathButton.onClick.RemoveListener(OnClearPathClicked);
        }
        
        if (toggleHighlightButton != null)
        {
            toggleHighlightButton.onClick.RemoveListener(OnToggleHighlightClicked);
        }
    }
} 